﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using Maticsoft.Model;
namespace Maticsoft.BLL
{
	/// <summary>
	/// workFlow
	/// </summary>
	public partial class workFlow
	{
		private readonly Maticsoft.DAL.workFlow dal=new Maticsoft.DAL.workFlow();
		public workFlow()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int workFlow_id)
		{
			return dal.Exists(workFlow_id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(Maticsoft.Model.workFlow model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Maticsoft.Model.workFlow model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int workFlow_id)
		{
			
			return dal.Delete(workFlow_id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string workFlow_idlist )
		{
			return dal.DeleteList(workFlow_idlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Maticsoft.Model.workFlow GetModel(int workFlow_id)
		{
			
			return dal.GetModel(workFlow_id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public Maticsoft.Model.workFlow GetModelByCache(int workFlow_id)
		{
			
			string CacheKey = "workFlowModel-" + workFlow_id;
			object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(workFlow_id);
					if (objModel != null)
					{
						int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
						Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (Maticsoft.Model.workFlow)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Maticsoft.Model.workFlow> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<Maticsoft.Model.workFlow> DataTableToList(DataTable dt)
		{
			List<Maticsoft.Model.workFlow> modelList = new List<Maticsoft.Model.workFlow>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				Maticsoft.Model.workFlow model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
					}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod
         //获取数据(判断是否需要会签，如果返回多条则需要会签)
        public DataSet GetWorkFlow(int flow_id, int flow_node_num)
        {
            return dal.GetWorkFlow(flow_id, flow_node_num);
        }

        /// <summary>
        /// 查询待办审批公文
        /// </summary>
        /// <returns></returns>
        public DataSet GetDaiBanCheck(string workFlow_status, string person_id,int isWork,string flow_op)
        {
            return dal.GetDaiBanCheck(workFlow_status, person_id, isWork,flow_op);
        }
         /// <summary>
        /// 查询待办审批公文(没有内联approver)
        /// </summary>
        /// <returns></returns>
        public DataSet GetDaiBanCheckByPerson(string workFlow_status, string person_id, int isWork, string flow_op)
        {
            return dal.GetDaiBanCheckByPerson(workFlow_status, person_id, isWork, flow_op);
        }
         /// <summary>
        /// 查询待办审批公文
        /// </summary>
        /// <returns></returns>
        public DataSet GetYiBanCheck(string person_id, int isWork, string flow_op)
        {
            return dal.GetYiBanCheck(person_id,isWork,flow_op);
        }
          //获取下一步审核人节点ID
        public int GetFlow_node_id(int person_id, int document_id, int flow_node_num,int flow_id)
        {
            return dal.GetFlow_node_id(person_id, document_id, flow_node_num,flow_id);
        }
       
        
        /// <summary>
        /// 在workFlow表中修改本身意见
        /// </summary>
        /// <param name="person_id"></param>
        /// <param name="document_id"></param>
        /// <param name="workFlow_status"></param>
        /// <param name="workFlow_reson"></param>
        /// <param name="time"></param>
        /// <param name="flow_node_id">当前节点</param>
        /// <param name="flow_node_num">当前步骤</param>
        /// <returns></returns>
        public bool UpdateW(int person_id, int document_id, int workFlow_status, string workFlow_reson, DateTime time, int flow_node_id_now, int flow_node_num_now)
        {
            return dal.Update(person_id, document_id, workFlow_status, workFlow_reson, time, flow_node_id_now, flow_node_num_now);
        }

         /// <summary>
        /// 在workFlow表中修改本身意见
        /// </summary>
        /// <param name="person_id"></param>
        /// <param name="document_id"></param>
        /// <param name="workFlow_status"></param>
        /// <param name="workFlow_reson"></param>
        /// <param name="time"></param>
        /// <param name="flow_node_id">当前节点</param>
        /// <param name="flow_node_num">当前步骤</param>
        /// <returns></returns>
        public bool Update(int workFlow_status, string workFlow_reson, DateTime time, string workFlow_id)
        {
            return dal.Update(workFlow_status, workFlow_reson, time, workFlow_id);
        }


        /// <summary>
        /// 提交状态判断
        /// </summary>
        /// <param name="flow_node_id"></param>
        /// <returns>同意、否决、非会签、尾节点同意、尾节点否决、尾节点非会签</returns>
        public string Submit_judge(int flow_node_id, int person_id, int document_id, int flow_node_num)
        {
            string str = "";

            Maticsoft.Model.flow_node node = new Maticsoft.BLL.flow_node().GetModel(flow_node_id);

            if (null != node)
            {
                if (node.flow_node_isLast == false)//判断是否是尾节点 0、否，1、是
                {
                    if (node.flow_node_isMulti == 1)//判断此节点是否会签 0、否，1、是
                    {
                        if (node.flow_node_all == 1)//如果条件为有状态时就执行（只要有一个状态就执行，0不使用此条件）
                        {
                            str = AllStatus(person_id, document_id, flow_node_num, flow_node_id);
                        }
                        if (node.flow_node_allapprove == 1)//如果条件为所有状态一致时执行
                        {
                            str = Allapprove(person_id, document_id, flow_node_num, flow_node_id);
                        }
                        if (node.flow_node_preserving == 1)//如果条件为大多数状态执行
                        {

                        }
                    }
                    else
                    {
                        str = "非会签";
                    }

                }
                else//是尾节点
                {
                    if (node.flow_node_isMulti == 1)//判断此节点是否会签 0、否，1、是
                    {
                        if (node.flow_node_all == 1)//如果条件为有状态时就执行（只要有一个状态就执行，0不使用此条件）
                        {
                            str = "尾节点" + AllStatus(person_id, document_id, flow_node_num, flow_node_id);
                        }
                        if (node.flow_node_allapprove == 1)//如果条件为所有状态一致时执行
                        {
                            str = "尾节点" + Allapprove(person_id, document_id, flow_node_num, flow_node_id);
                        }
                        if (node.flow_node_preserving == 1)//如果条件为大多数状态执行
                        {

                        }
                    }
                    else
                    {
                        str = "尾节点非会签";
                    }
                }

            }

           
            return str;
        }

        //有一个状态时就执行，只要有一个状态（同意）就执行
        private string AllStatus(int person_id, int document_id, int flow_node_num, int flow_node_id)
        {
            string str = "";
            Maticsoft.DAL.workFlow w = new Maticsoft.DAL.workFlow();

            int count0 = w.GetCount(0, person_id, document_id, flow_node_num, flow_node_id);//未审批
            int count1 = w.GetCount(1, person_id, document_id, flow_node_num, flow_node_id);//同意
            int count2 = w.GetCount(2, person_id, document_id, flow_node_num, flow_node_id);//否决
            //int count3 = w.GetCount(3, person_id, document_id, flow_node_num,flow_node_id);//退签
            int count = w.GetCountAll(person_id, document_id, flow_node_num, flow_node_id);// 总人数

            if (count1 > 0)
            {
                str = "同意";
            }
            else
            {
                if (count2 == count)
                {
                    str = "否决";
                }
                else if (count0 == count)
                {
                    str = "未审批";
                }
                else
                {
                    str = "同意";
                }
            }


            return str;
        }

        //所有状态一致时执行(一票否决权)
        private string Allapprove(int person_id, int document_id, int flow_node_num, int flow_node_id)
        {
            string str = "";
            Maticsoft.DAL.workFlow w = new Maticsoft.DAL.workFlow();

            int count0 = w.GetCount(0, person_id, document_id, flow_node_num, flow_node_id);//未审批
            int count1 = w.GetCount(1, person_id, document_id, flow_node_num, flow_node_id);//同意
            int count2 = w.GetCount(2, person_id, document_id, flow_node_num, flow_node_id);//否决
            //int count3 = w.GetCount(3, person_id, document_id, flow_node_num,flow_node_id);//退签
            int count = w.GetCountAll(person_id, document_id, flow_node_num, flow_node_id);// 总人数

            if (count0 > 0)
            {
                str = "未审批";
            }
            else
            {
                if (count1 == count)
                {
                    str = "同意";
                }
                else
                {
                    str = "否决";
                }
            }

            return str;


        }

        //根据节点号 获取流程下此节点号的所有节点
        public DataSet GetFlowNodeByFlow_idAndFlow_node_num(int flow_id, int flow_node_num)
        {
            return dal.GetFlowNodeByFlow_idAndFlow_node_num(flow_id, flow_node_num);
        }
        //根据节点号 获取流程下此节点号的所有节点(手工分发)
        public DataSet GetFlowNodeByFlow_idAndFlow_node_numSend(int flow_id, int flow_node_num)
        {
            return dal.GetFlowNodeByFlow_idAndFlow_node_numSend(flow_id,flow_node_num);
        }
        
        //获取当前审核人节点
        public int GetFlow_node_id_now(int person_id, int document_id, int flow_node_num)
        {
            return dal.GetFlow_node_id_now(person_id, document_id, flow_node_num);
        }
         //根据节点号 获取流程下此节点号的所有节点
        public DataSet GetFlowInfo(int document_id)
        {
            return dal.GetFlowInfo(document_id);
        }

        /// <summary>
        /// 查询待办招标计划审核公文 
        /// </summary>
        /// <returns></returns>
        public DataSet GetDaiBanBidCheck(string workFlow_status, string person_id,string flow_op)
        {
            return dal.GetDaiBanBidCheck(workFlow_status, person_id, flow_op);
        }

        /// <summary>
        /// 查找审核的上一步信息
        /// </summary>
        /// <param name="document_id"></param>
        /// <param name="flow_node_id"></param>
        /// <param name="flow_node_num_now"></param>
        /// <returns></returns>
        public DataSet GetLastWorkFlowInfo(string document_id, string flow_node_id, int flow_node_num_now)
        {
            return dal.GetLastWorkFlowInfo(document_id, flow_node_id, flow_node_num_now);
        }
         /// <summary>
        /// 查找审核信息
        /// </summary>
        /// <param name="document_id"></param>
        /// <param name="flow_node_id"></param>
        /// <param name="flow_node_num_now"></param>
        /// <returns></returns>
        public DataSet GetNowWorkFlowInfo(string document_id, string flow_node_id_now, int flow_node_num_now)
        {
            return dal.GetNowWorkFlowInfo(document_id, flow_node_id_now, flow_node_num_now);
        }
        /// <summary>
        /// 获取审核详细信息从视图VworkFlowUser
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetWorkFlowInfo(string strWhere)
        {
            return dal.GetWorkFlowInfo(strWhere);
        }
		#endregion  ExtensionMethod
	}
}

